package com.yulinlin.admin.modules.auth;

import com.yulinlin.admin.modules.sys.domain.SysElementEntity;
import com.yulinlin.admin.modules.sys.domain.SysElementRelEntity;
import com.yulinlin.admin.modules.sys.domain.join.SysElementRelJoin;
import com.yulinlin.admin.modules.sys.service.ISysElementRelService;
import com.yulinlin.authorization.feign.JwtToken;
import com.yulinlin.authorization.feign.OAuth2FeignClient;
import com.yulinlin.authorization.util.OauthUtil;
import com.yulinlin.common.anno.JwtElement;
import com.yulinlin.common.domain.RequestPo;
import com.yulinlin.common.domain.ResponseVo;
import com.yulinlin.data.core.model.BaseModelDeleteWrapper;
import com.yulinlin.data.core.model.BaseModelSelectWrapper;
import com.yulinlin.jdbc.session.JdbcSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "授权")
@RestController
@RequestMapping("auth")
public class AuthController  {

    @Autowired
    private OAuth2FeignClient oAuth2FeignClient;

    @Autowired
    private JdbcSession  jdbcSession;

    @Autowired
    private ISysElementRelService iSysElementRelService;

    private  String basicAuth =  OauthUtil.authorization("admin","admin");

    private String loginType="admin";

    private String grantType="password";

    @ApiOperation("登录")
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public ResponseVo<String> login(@RequestBody  RequestPo< LoginPo> data){
        LoginPo po =   data.getData();
        ResponseEntity<JwtToken> responseEntity = oAuth2FeignClient.login(grantType,po.getUsername(),po.getPassword(),loginType,basicAuth);
        if(responseEntity.getStatusCode() == HttpStatus.OK){
            return ResponseVo.newInstance("bearer "+responseEntity.getBody().getAccessToken());
        }else{
           return   ResponseVo.newInstance(null,401,"账号或密码错误");
        }
    }

    @ApiOperation("注销")
    @RequestMapping(value = "logout",method = RequestMethod.POST)
    public ResponseVo<String> logout(){
        return ResponseVo.newInstance(null,"注销成功");
    }



    @Autowired
    RedisTemplate redisTemplate;


    @JwtElement
    @ApiOperation("获取用户详情")
    @RequestMapping(value = "getUserInfo",method = RequestMethod.POST)
    public  ResponseVo<UserInfoVo>  getUserInfo(){

        String userId =  OauthUtil.getUserId();

        UserInfoVo vo = (UserInfoVo) redisTemplate.opsForValue().get(userId);

        if(vo == null){
            vo =    new UserInfoVo(userId);
            vo = jdbcSession.getLazyProxy(vo);
        }


        int total =  (int) BaseModelSelectWrapper.newInstance(SysElementEntity.class).count();
        if(vo.getSysElementList().size() != total){
            List<SysElementRelJoin> relList =  iSysElementRelService.initElementRel(userId);
            vo.setSysElementList(relList);

            redisTemplate.opsForValue().set(userId,vo);
        }



        return ResponseVo.newInstance(vo,"获取详情成功");
    }


}
